package com.bangdream.michelia.view.activity.curriculum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.courses.TeacherListBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.bangdream.michelia.view.adapter.CourseItemAdapter;
import com.bangdream.michelia.view.adapter.CourseListAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CourseListActivity extends SwipeBackActivity<CoursesContract.ICoursesView, CoursesPresenter<CoursesContract.ICoursesView>>
        implements CoursesContract.ICoursesView {

    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    CourseItemAdapter adapter;

    private List<CoursesBean> dataList;
    private XRefreshView refreshView;
    private int page = 1;


    @Override
    public int getLayout() {
        return R.layout.activity_course_list;
    }

    @Override
    public void findId() {
        refreshView = findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(true);
        refreshView.setPullLoadEnable(false);
        refreshView.setAutoRefresh(true);
        recyclerView = findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new CourseItemAdapter(R.layout.curriculum_adapter_item);
        recyclerView.setAdapter(adapter);

        String courseType = getIntent().getStringExtra("courseType");
        String titleName = "";
        switch (courseType) {
            case "1":
                titleName = "免费课程";
                break;
            case "2":
                titleName = "最新推荐";
                break;
            case "3":
                titleName = "推荐课程";
                break;
            case "4":
                titleName = "全部课程";
                break;

        }
        initTitleBar(" ", titleName, "", this);

        loadData();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(CourseListActivity.this,CurriculumDetails.class);
                intent.putExtra("id",adapter.getData().get(position).getId());
                intent.putExtra("title",adapter.getData().get(position).getName());
                List<TeacherListBean> teacherList = adapter.getData().get(position).getTeacherList();
                if(teacherList!=null && teacherList.size()>0)
                    intent.putExtra("teacherId",adapter.getData().get(position).getTeacherList().get(0).getTeacherId());
                startActivity(intent);

            }
        });
    }

    @Override
    public void setListener() {
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });
    }

    private void loadData() {
        Map params = new HashMap();
        params.put("status","0");//0上架，1下架
        params.put("delFlag","0");//（0正常、1回收站、2、彻底删除）
        mPresenter.getCoursesListData(
                page, PublicResources.PageSize, CoursesContract.CoursesType.Ordinary, params, null);

    }

    @Override
    public void logic() {

    }


    @Override
    public CoursesPresenter<CoursesContract.ICoursesView> createPresenter() {
        return new CoursesPresenter<>();
    }


    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {
        if (isSuccess && data != null) {
            if (page == 1) {
                dataList = data;
                adapter.setNewData(dataList);
                adapter.notifyDataSetChanged();
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if (dataList.size() >= PublicResources.PageSize) {
                    refreshView.setPullLoadEnable(true);
                }
            } else {
                int tempIndex = adapter.getData().size();
                for (int i = 0; i < data.size(); i++) {
                    adapter.addData(data.get(i));
                }
                adapter.notifyItemChanged(tempIndex, adapter.getData().size());
                refreshView.stopLoadMore();
            }
        } else {
            //如果是加载新数据时失败了 则page-1
            if (page > 1) {
                refreshView.stopLoadMore();
                this.page--;
            } else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
            }
        }

    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {

    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {

    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {

    }
}
